package io.renren.modules.clearing.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.LwClientHelper;
import cn.com.taiji.sdk.model.comm.protocol.cdts.passTransaction.ETCClearDownloadRequest;
import cn.com.taiji.sdk.model.comm.protocol.cdts.passTransaction.ETCClearItemModel;
import io.renren.common.utils.*;
import io.renren.modules.basedata.entity.XJException;
import io.renren.modules.clearing.dao.*;
import io.renren.modules.clearing.entity.*;
import io.renren.modules.clearing.service.ETCClearingInfoDownService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

@Service
public class ETCClearingInfoDownServiceImpl implements ETCClearingInfoDownService {
    private Logger log = LoggerFactory.getLogger(getClass());
    
    @Autowired
    ETCAccountTradeDownDao etcAccountTradeDownDao;
    @Autowired
    ConfigParaDicDao configParaDicDao;
    @Autowired
    DataExchangeListDao dataExchangeListDao;
    @Autowired
    DownVersionDicDao downVersionDicDao;
    @Autowired
    ZFKExitWasteDao zfkExitWasteDao;
    @Autowired
    ETCClearingInfoDownDao etcClearingInfoDownDao;
    @Autowired
    DownErrorVersionDao downErrorVersionDao;
    @Override
    public String[] ETCClearingInfoDown(String sender) throws Exception {

        // TODO Auto-generated method stub
        String msg="";
        String version=null;
        String[] versions=null;
        log.info("开始检查控制参数");
        //检查控制参数是否下载 tbl_configparadic
        int control = configParaDicDao.checkContolParam(Constants.CONTROL_ETCAUDOWN_PARAM);
       // int control=(Integer) sqlMapClient.queryForObject("Base_ORACLE.checkContolParam",Constants.CONTROL_ETCCIDDOWN_PARAM);
        if(control==1){
            //获取要需下载的版本号
            version = downVersionDicDao.getVersion(Constants.DOWNLOAD_TYPE_ETCACCOUNTTRADEDOWNLOAD);
            //version=(String) sqlMapClient.queryForObject("Base_ORACLE.getVersion",sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN);
            if(version!=null ){
                //判断有多少版本号要处理
                versions=TypeTransUtil.checkVersion(version, Constants.DOWNLOAD_FILE_1D);
            }
        }else{
            msg="接口暂停，任务退出";
            log.info(msg);
        }
        return versions;

    }
    /**
     * 根据版本号请求记账交易下载
     * @param version
     * @return
     * @throws Exception
     */
    @Override
    public boolean DownloadFile(String version, String sender, int type) throws Exception {

        boolean flag=false;
        ETCClearDownloadRequest request = new ETCClearDownloadRequest();
        request.setClearDate(version);
        request.setReqSender(sender);
        log.info("开始执行版本:"+version);
        String path=";文件路径:";
        try {
            File file =LwClientHelper.downLoad(request, Constants.DOWNLOAD_PATH_ETCCLEARING);
//			File file =new File("E:\\download\\ETCTransaction\\650201_20181109.zip");
            System.out.println("ETC清分结算文件交易下载成功，下载文件为："+file.getAbsolutePath());
            path=path+file.getAbsolutePath();
            //解压文件
            String filedir=file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf("."));
            ZipUncompress.unZip(file.getAbsolutePath(),filedir);
            //获取解压目录下的所有文件
            List<File> filelist = FileTool.getFileList(filedir);
            log.info("解压文件共"+filelist.size()+"个json文件");
            if(filelist.size()==0){
                log.info("空包文件，退出任务");
                return true;
            }
            //循环解析所有文件入库
            String fileName=null;
            for (File f :filelist) {
                fileName = f.getName();
                log.info("开始解析"+fileName+"文件");
                String filejsonstr=	FileTool.getjsonStr(filedir+"\\"+fileName);
                //log.info(fileName+"文件内容："+filejsonstr);
                //将json串转对象
                ETCClear model=FileTool.parseJsonToObj(filejsonstr, ETCClear.class);
                if(model!=null){
                    //文件校验
                    //交易文件数量
                    int messageCount=0;
                    //结算总金额
                    long amount=0;
                    //争议处理结果文件数量
                    int disputedCount=0;




                    //
                    List<ETCClearDetail>  ETCClearDetail = new ArrayList<ETCClearDetail>();
                    ETCClearDetail=model.getClearDetail();
                    if(ETCClearDetail==null||ETCClearDetail.size()==0){
                        log.info(fileName+"文件内容清分明细为空");
                        throw	new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败："+fileName+"文件内容清分明细为空"+path, 4);
                    }

                    //循环解析文件将服务方相同发行方相同，通行方不同数据进行整合
                    //数据库原始交易为一条数据，清分明细会按照通行方进行拆分
                    //比如一条交易110为山东收费 会按通行方拆分为统一包号的两条数据
                    /**
                     for (int i = 0; i < ETCClearDetail.size(); i++) {
                     ETCClearDetail detail= ETCClearDetail.get(i);
                     for (int j = 0; j < ETCClearDetail.size(); j++) {
                     ETCClearDetail detail2= ETCClearDetail.get(j);
                     if(detail.getSerProvinceId().equals(detail.getTollProvinceId())&&detail.getTollProvinceId().equals(detail2.getTollProvinceId())&& detail.getIssuerId().equals(detail2.getIssuerId())&& !detail.getSerProvinceId().equals(detail2.getSerProvinceId())){
                     List<ETCClearItemModel> itemList = detail.getClearItems();
                     List<ETCClearItemModel> itemList2 = detail2.getClearItems();
                     for (int k = 0; k < itemList.size(); k++) {
                     ETCClearItemModel item = itemList.get(k);
                     for (int l = 0; l < itemList2.size(); l++) {
                     ETCClearItemModel item2 = itemList2.get(l);
                     //判断包号是否相同，如果相同将第二个通行方数据合并到通行方和服务方相同的数据中
                     if(item2.getFileId().equals(item.getFileId())){
                     item.setAmount(item.getAmount()+item2.getAmount());
                     itemList2.remove(item2);
                     detail2.setMessageCount(detail2.getMessageCount()-1);
                     detail.setAmount(detail.getAmount()+item2.getAmount());
                     detail2.setAmount(detail2.getAmount()-item2.getAmount());
                     }
                     }
                     }

                     }
                     }

                     }
                     */
//			    	for (int i = 0; i < ETCClearDetail.size(); i++) {
//			    		messageCount+=ETCClearDetail.get(i).getMessageCount();
//			    		disputedCount+=ETCClearDetail.get(i).getDisputedCount();
//			    		amount+=ETCClearDetail.get(i).getAmount();
//					}
//			    	//交易文件和文件明细是否一致
//			    	if(messageCount!=model.getMessageCount()||disputedCount!=model.getDisputedCount()||amount!=model.getAmount()){
//			    		log.info(fileName+"文件内容总记录数和清分明细不一致");
//			    		new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败："+fileName+"文件内容总记录数和清分明细不一致", 4);
//			    	}

                    for (int i = 0; i < ETCClearDetail.size(); i++) {
                        //清分项目交易文件数量
                        int messageCountItem=0;
                        //清分项目结算总金额
                        long amountItem=0;
                        //清分项目争议处理结果文件数量
                        int disputedCountItem=0;
                        ETCClearDetail etcClearDetail =ETCClearDetail.get(i);
                        List<ETCClearItemModel> itemList = etcClearDetail.getClearItems();
                        for (int j = 0; j < itemList.size(); j++) {
                            ETCClearItemModel item = itemList.get(j);
                            //计算item中的数量
                            if(item.getFileType()==0){
                                messageCountItem++;
                            }else if(item.getFileType()==1){
                                disputedCountItem++;
                            }
                            amountItem+=item.getAmount();

                        }
                        //判断是否和上级节点一致
                        if(messageCountItem!=etcClearDetail.getMessageCount()||disputedCountItem!=etcClearDetail.getDisputedCount()||amountItem!=etcClearDetail.getAmount()){
                            log.info(fileName+"文件内容ClearDetail总记录数和ClearItem清分明细不一致");
                            throw new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败："+fileName+"文件内容ClearDetail总记录数和ClearItem清分明细不一致"+path, 4);
                        }
                    }

                    //校验数据库记录  判断清分是否以及存在
                    Map checkClear = new HashMap();
                    checkClear.put("ClearDate", model.getClearDate());
                    checkClear.put("ServiceType", 1);
                    checkClear.put("type", sender.equals(Config.tollProvinceId)?2:1);//2-服务方清分1-发行方清分
                    int ClearDateCount= etcClearingInfoDownDao.checkClearByClearDate(checkClear);
                  //  int ClearDateCount= (Integer) sqlMapClient.queryForObject("ZfkExitW_ORACLE.checkClearByClearDate",checkClear);
                    if(ClearDateCount>0){
                        log.info("清分日期："+model.getClearDate()+"清分数据已存在");
                        throw new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：清分日期："+model.getClearDate()+"清分数据已存在"+path, 4);
                    }
                    //服务方请求
                    for (int i = 0; i < ETCClearDetail.size(); i++) {
                        ETCClearDetail etcClearDetail =ETCClearDetail.get(i);
                        List<ETCClearItemModel> itemList = etcClearDetail.getClearItems();
                        for (int j = 0; j < itemList.size(); j++) {
                            ETCClearItemModel item = itemList.get(j);
                            Map map = new HashMap();
                            map.put("messageID", item.getFileId());
                            map.put("issueID",  TypeTransUtil.issueIDTonetID(etcClearDetail.getIssuerId()));
                            map.put("userID",  sender.equals(Config.tollProvinceId)?0:etcClearDetail.getTollProvinceId());
                            map.put("serID",  etcClearDetail.getSerProvinceId());
                            //更新清分日期
                            map.put("clearDate", model.getClearDate());
                            map.put("fileType", item.getFileType());

                            //原始交易
                            if(item.getFileType()==0){
                                //检查交易情况
                               // ETCClearItem it=(ETCClearItem) sqlMapClient.queryForObject("ZfkExitW_ORACLE.checkClearTranstion",map);
                                ETCClearItem it=etcClearingInfoDownDao.checkClearTranstion(map);
                                if(it!=null){
//				    				if(!it.getCount().equals(item.getCount())){
//				    					log.info("原始交易:"+item.getFileId()+"清分总记录数与系统总记录数不一致");
//				    					throw new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：原始交易:"+item.getFileId()+"清分总记录数"+item.getCount()+"与系统总记录数"+it.getCount()+"不一致"+path, 4);
//				    				}if(!it.getAmount().equals(item.getAmount())){
//				    					log.info("原始交易:"+item.getFileId()+"清分总金额与系统总金额不一致");
//				    					throw	new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：原始交易:"+item.getFileId()+"清分总金额"+item.getAmount()+"与系统总金额"+it.getAmount()+"不一致"+path, 4);
//				    				}
                                    if(etcClearDetail.getSerProvinceId().equals(etcClearDetail.getTollProvinceId())){
                                        if(sender.equals(Config.tollProvinceId) && !it.getCount().equals(item.getCount())){//服务方清算
                                            log.info("原始交易:"+item.getFileId()+"清分总记录数与系统总记录数不一致");
                                            throw new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：原始交易:"+item.getFileId()+"清分总记录数"+item.getCount()+"与系统总记录数"+it.getCount()+"不一致"+path, 4);
                                        }
                                        if(!item.getAmount().equals(it.getAmount2()+it.getAmount())){
                                            log.info("原始交易:"+item.getFileId()+"清分总金额与系统总金额不一致");
                                            throw	new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：原始交易:"+item.getFileId()+"清分总金额"+item.getAmount()+"与系统总金额"+(it.getAmount2()+it.getAmount())+"不一致"+path, 4);
                                        }
                                    }else{
                                        if(!it.getAmount().equals(item.getAmount())){
                                            log.info("原始交易:"+item.getFileId()+"清分总金额与系统总金额不一致");
                                            throw	new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：原始交易:"+item.getFileId()+"清分总金额"+item.getAmount()+"与系统总金额"+it.getAmount()+"不一致"+path, 4);
                                        }
                                    }
                                }
                                //更新清分日期 修改清分日期格式为20181225
                                map.put("tranwastesn", model.getClearDate().replaceAll("-", ""));
                                etcClearingInfoDownDao.editClearDate(map);
                                //sqlMapClient.update("ZfkExitW_ORACLE.editClearDate",map);

                            }else if(item.getFileType()==1){//争议交易
                                //检查交易情况
                                //ETCClearItem it=(ETCClearItem) sqlMapClient.queryForObject("ZfkExitW_ORACLE.checkClearByDispute",map);
                                ETCClearItem it=etcClearingInfoDownDao.checkClearByDispute(map);
                                if(it!=null){
//				    				if(!it.getCount().equals(item.getCount())){
//				    					log.info("争议交易:"+item.getFileId()+"清分总记录数与系统总记录数不一致");
//				    					throw	new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：争议交易:"+item.getFileId()+"清分总记录数"+item.getCount()+"与系统总记录数"+it.getCount()+"不一致"+path, 4);
//				    				}if(!it.getAmount().equals(item.getAmount())){
//				    					log.info("争议交易:"+item.getFileId()+"清分总金额与系统总金额不一致");
//				    					throw	new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：争议交易:"+item.getFileId()+"清分总金额"+item.getAmount()+"与系统总金额"+it.getAmount()+"不一致"+path, 4);
//				    				}
                                    if(etcClearDetail.getSerProvinceId().equals(etcClearDetail.getTollProvinceId())){
                                        if(sender.equals(Config.tollProvinceId) &&!it.getCount().equals(item.getCount())){
                                            log.info("争议交易:"+item.getFileId()+"清分总记录数与系统总记录数不一致");
                                            throw	new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：争议交易:"+item.getFileId()+"清分总记录数"+item.getCount()+"与系统总记录数"+it.getCount()+"不一致"+path, 4);
                                        }
                                        if(!item.getAmount().equals(it.getAmount2()+it.getAmount())){
                                            log.info("争议交易:"+item.getFileId()+"清分总金额与系统总金额不一致");
                                            throw	new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：争议交易:"+item.getFileId()+"清分总金额"+item.getAmount()+"与系统总金额"+(it.getAmount2()+it.getAmount())+"不一致"+path, 4);
                                        }
                                    }else{
//				    					if(!it.getAmount().equals(item.getAmount())){
//					    					log.info("争议交易:"+item.getFileId()+"清分总金额与系统总金额不一致");
//					    					throw	new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：争议交易:"+item.getFileId()+"清分总金额"+item.getAmount()+"与系统总金额"+it.getAmount()+"不一致"+path, 4);
//					    				}
                                    }
                                }
                                //更新清分日期 修改清分日期格式为20181225
                                map.put("tranwastesn", model.getClearDate().replaceAll("-", ""));
                                etcClearingInfoDownDao.editClearDateZY(map);
                                //sqlMapClient.update("ZfkExitW_ORACLE.editClearDateZY",map);
                            }



                            //循环入库
                            Map insertmap = new HashMap();
                            insertmap.put("clearDate", model.getClearDate());
                            insertmap.put("operatorId", etcClearDetail.getTollProvinceId());
                            insertmap.put("serProvinceId", etcClearDetail.getSerProvinceId());
                            insertmap.put("issuerId", etcClearDetail.getIssuerId());
                            insertmap.put("fileID",item.getFileId() );
                            insertmap.put("serviceType",1 );
                            insertmap.put("fileType",item.getFileType() );
                            insertmap.put("count",item.getCount() );
                            insertmap.put("amount",item.getAmount());
                            insertmap.put("ProcessTime",DateUtil.formatDateTime(DateUtil.formatDateTime6(model.getProcessTime())));
                            insertmap.put("type",sender.equals(Config.tollProvinceId)?2:1);
                            etcClearingInfoDownDao.addTransClear(insertmap);
                           // sqlMapClient.insert("transaction_ORACLE.addTransClear",insertmap);
                        }

                    }

                    //Tbl_DataExchangeList插入
                    DataExchangeList dataExchangeList=addDataExchangeList(model,fileName,sender);
                    dataExchangeListDao.addDataExchangeList(dataExchangeList);
                    //sqlMapClient.insert("Base_ORACLE.addDataExchangeList",dataExchangeList);
                    //插入日志
                    //DataExchangeLog dataLog = addLog(model,1, "ETC清分结算文件下载成功",sender);
                   // sqlMapClient.insert("Base_ORACLE.addDataExchangeLog",dataLog);
                    log.info("结束解析"+fileName+"清分文件文件");
                }else{
                    log.info("解析"+fileName+"文件转换对象失败");
                    //DataExchangeLog dataLog = addErrorLog(0, "解析"+fileName+"文件转换对象失败");
                    //sqlMapClient.insert("Base_ORACLE.addDataExchangeLog",dataLog);
                    throw new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：解析"+fileName+"文件转换对象失败"+path, 4);
                }
                //删除文件
                f.delete();
            }

            if(type==1){
                addDownErrorVersion(version, sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, "ETC清分结算文件下载请求响应成功"+file.getAbsolutePath(),0);
                //修改下载版本号
                version=TypeTransUtil.getNextVersion(version, Constants.DOWNLOAD_FILE_1D);
                Map map2 = new HashMap<>();
                map2.put("version", version);
                map2.put("MsgType", sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN);
                downVersionDicDao.editVersion(map2);
                //sqlMapClient.update("Base_ORACLE.editVersion",map2);
            }

            //删除文件夹
            File F = new  File(filedir);
            F.delete();
            flag=true;
        } catch (IOException e) {
            // TODO: handle exception
            log.info("ETC清分结算文件下载请求响应失败,版本号："+version+":"+e.getMessage());
            throw new Exception(new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败IO异常,版本号："+version+path+":"+e.getMessage(), 1));
        } catch (ApiRequestException apie) {
            //TODO 系统自定义异常处理，各省补全
            apie.printStackTrace();
            if(apie.getErrCode()==Constants.EMPTYDATACODE){
                log.info("ETC清分结算文件下载请求误数据711,版本号："+version+":正常退出");
                if(type==1){
                    addDownErrorVersion(version, sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, "ETC清分结算文件下载请求响应成功误数据",0);
                    //修改下载版本号
                    version=TypeTransUtil.getNextVersion(version, Constants.DOWNLOAD_FILE_1D);
                    Map map2 = new HashMap<>();
                    map2.put("version", version);
                    map2.put("MsgType", sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN);
                    downVersionDicDao.editVersion(map2);
                    //sqlMapClient.update("Base_ORACLE.editVersion",map2);
                }
                flag=true;
            }else{
                log.error("ETC清分结算文件下载请求响应失败,版本号："+version+"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
                //添加版本异常表
                throw new Exception(new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败：版本号："+version+"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage()+path, apie.getErrCode()));
            }
        }catch (XJException e) {
            throw new Exception(e);
        }catch (SQLException e) {
            String msg=TypeTransUtil.getErrorMessage(e.getMessage()) ;
            throw new Exception(new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败:"+msg+path, 7));
        }catch (Exception e) {
            //添加版本异常表
            String msg=TypeTransUtil.getErrorMessage(e.getMessage()) ;
            throw new Exception(new XJException(sender==Config.issuerId?Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN_ISSUEID:Constants.DOWNLOAD_TYPE_ETCCLEARINGINFODOWN, version, "ETC清分结算文件下载请求响应失败："+msg+path, 3));
        }
        return flag;

    }

    /**
     * 添加异常版本信息
     * @param version
     * @param reason
     * @param flag 0异常错误 ；1校验错误
     * @throws Exception
     */
    public void addDownErrorVersion(String version,int type,String reason,int flag)throws Exception{
        //添加版本异常表
        DownErrorVersion dv=TypeTransUtil.addDownErrorVersion(version, type, reason);
        dv.setFlag(flag);
        int count = downErrorVersionDao.selectDownErrorVersion(dv);
       // int count =(Integer) sqlMapClient.queryForObject("Base_ORACLE.selectDownErrorVersion",dv);
        if(count>0){
            downErrorVersionDao.editDownErrorVersion(dv);
           // sqlMapClient.update("Base_ORACLE.editDownErrorVersion",dv);
        }else{
            downErrorVersionDao.addDownErrorVersion(dv);
            //sqlMapClient.insert("Base_ORACLE.addDownErrorVersion",dv);
        }
    }
    /**
     * 添加全国联网记录
     * @param
     * @param
     * @return
     * @throws Exception
     */
    public DataExchangeList addDataExchangeList( ETCClear model,String fileName,String sender)throws Exception{
        Date nowTime=  new Date();
        DataExchangeList dataExchangeList= new DataExchangeList();
        dataExchangeList.setMessageID("0");
        dataExchangeList.setSenderID(sender);
        dataExchangeList.setReceiverID(sender);
        dataExchangeList.setProcessTime(nowTime);
        dataExchangeList.setMessageClass(2);
        dataExchangeList.setMessageType(4);
        dataExchangeList.setContentType(1);
        dataExchangeList.setResult(1);
        dataExchangeList.setRecCount(model.getMessageCount());
        dataExchangeList.setRecAmount(0);
        dataExchangeList.setVersion(0);
        dataExchangeList.setReplyFlag(1);
        dataExchangeList.setReplyTime(nowTime);
        dataExchangeList.setFileName(fileName);
        dataExchangeList.setResultDesc("ETC清分结算文件下载成功");
        dataExchangeList.setSpare1("0");
        dataExchangeList.setSpare3("ETC清分结算文件下载");
        return dataExchangeList;

    }
}
